package com.cqeec.gams.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cqeec.gams.entity.Account;
import com.cqeec.gams.entity.AccountRole;
import com.cqeec.gams.entity.Role;
import com.cqeec.gams.service.AccountService;
import com.cqeec.gams.service.RoleService;

@Controller
@RequestMapping("/app/user/")
public class UserController {

	@Autowired
	private AccountService accountService;
	@Autowired
	private RoleService roleService;

	@RequestMapping("/delete.html")
	public ModelAndView delete(String id, int page, int size) {
		ModelAndView mav = new ModelAndView("redirect:/app/user/list.html");
		accountService.delete(id);
		mav.addObject("page", page);
		mav.addObject("size", size);
		return mav;
	}

	@RequestMapping("/update.html")
	public ModelAndView update(Account user, String[] roles, int page, int size) {
		ModelAndView mav = new ModelAndView("user/edit");
		mav.addObject("page", page);
		mav.addObject("size", size);
		try {
			accountService.update(user);
			// 更新账户角色
			// 先删除账户原有的角色
			accountService.deleteAccountRole(user.getId());
			// 插入用户角色信息
			if (roles != null) {
				AccountRole ar;
				for (String s : roles) {
					ar = new AccountRole();
					ar.setId(UUID.randomUUID().toString());
					ar.setAccount(user.getId());
					ar.setRole(s);
					accountService.insertAccountRole(ar);
				}
			}
			// 查询所有角色
			List<Role> roleList = roleService.findAll();
			// 查询用户拥有的角色
			List<AccountRole> accountRoles = accountService.getAccountRole(user.getId());
			String roleId;
			for (AccountRole ar : accountRoles) {
				roleId = ar.getRole();
				for (Role r : roleList) {
					if (roleId.equals(r.getId())) {
						r.setChecked(true);
					}
				}
			}
			mav.addObject("roles", roleList);
			mav.addObject("message", "保存用户成功！");
		} catch (Exception e) {
			e.printStackTrace();
			// 查询所有角色
			List<Role> roleList = roleService.findAll();
			// 查询用户拥有的角色
			List<AccountRole> accountRoles = accountService.getAccountRole(user.getId());
			String roleId;
			for (AccountRole ar : accountRoles) {
				roleId = ar.getRole();
				for (Role r : roleList) {
					if (roleId.equals(r.getId())) {
						r.setChecked(true);
					}
				}
			}
			mav.addObject("roles", roles);
			mav.addObject("message", "保存用户失败！");
		}
		mav.addObject("user", user);
		return mav;
	}

	@RequestMapping("/save.html")
	public ModelAndView save(Account user, String[] roles, RedirectAttributes ra) {
		ModelAndView mav = new ModelAndView("user/add");
		String id = user.getId();
		if (id == null || "".equals(id)) {
			user.setId(UUID.randomUUID().toString().toUpperCase());
		}
		try {
			accountService.insert(user);
			// 插入用户角色信息
			if (roles != null) {
				AccountRole ar;
				for (String s : roles) {
					ar = new AccountRole();
					ar.setId(UUID.randomUUID().toString());
					ar.setAccount(user.getId());
					ar.setRole(s);
					accountService.insertAccountRole(ar);
				}
			}
			mav.setViewName("redirect:/app/user/add.html");
			ra.addFlashAttribute("message", "添加用户成功！");
		} catch (Exception e) {
			e.printStackTrace();
			// 查询所有角色
			mav.addObject("roles", roleService.findAll());
			mav.addObject("message", "添加用户失败！");
		}
		return mav;
	}

	@RequestMapping("/edit.html")
	public ModelAndView gotoEditPage(String id, int page, int size) {
		ModelAndView mav = new ModelAndView("user/edit");
		Account user = accountService.findById(id);
		mav.addObject("user", user);
		mav.addObject("page", page);
		mav.addObject("size", size);
		// 查询所有角色
		List<Role> roles = roleService.findAll();
		// 查询用户拥有的角色
		List<AccountRole> accountRoles = accountService.getAccountRole(id);
		String roleId;
		for (AccountRole ar : accountRoles) {
			roleId = ar.getRole();
			for (Role r : roles) {
				if (roleId.equals(r.getId())) {
					r.setChecked(true);
				}
			}
		}
		mav.addObject("roles", roles);
		return mav;
	}

	@RequestMapping("/add.html")
	public ModelAndView gotoAddPage() {
		ModelAndView mav = new ModelAndView("user/add");
		// 查询所有角色
		mav.addObject("roles", roleService.findAll());
		return mav;
	}

	@RequestMapping("/list.html")
	public ModelAndView gotoMainPage(String page, String size) {
		ModelAndView mav = new ModelAndView("user/list");
		int iPage, iSize;
		if (page == null || "".equals(page)) {
			iPage = 1;
		} else {
			iPage = Integer.parseInt(page);
		}
		if (size == null || "".equals(size)) {
			iSize = 10;
		} else {
			iSize = Integer.parseInt(size);
		}
		long total = accountService.count();
		mav.addObject("page", iPage);
		mav.addObject("pages", total % iSize == 0 ? total / iSize : total / iSize + 1);
		mav.addObject("size", iSize);
		mav.addObject("total", total);
		List<Account> list = accountService.findPage(iPage, iSize);
		mav.addObject("list", list);
		return mav;
	}

}
